﻿using JRCore.protocol;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace MCTXCLMQ
{

    public partial class Service1 : ServiceBase
    {
        public string gFTPfolder = null;
        public MQ mq = null;
        public string gMQUri;
        public string gTopicName;
        public Service1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 初始化
        /// </summary>
        private void Init()
        {
            try
            {
                gFTPfolder = ConfigurationManager.AppSettings["FTPfolder"];
                gMQUri = ConfigurationManager.AppSettings.Get("MQUri");       //MQurl
                gTopicName = ConfigurationManager.AppSettings.Get("TopicName");   //
    }
            catch (System.Exception ex)
            {
                LogHelper.WriteLog(typeof(Service1), ex.Message);

            }
            if (!Directory.Exists(gFTPfolder)) Directory.CreateDirectory(gFTPfolder);
            fileSystemWatcher1.Path = gFTPfolder;

            mq = new MQ(gMQUri, gTopicName);
            mq.Start();

        }
        protected override void OnStart(string[] args)
        {
            LogHelper.WriteLog(typeof(Service1), "杰瑞上传MQ服务启动");

        }

        protected override void OnStop()
        {
            if (mq != null)
            {
                mq.Close();
                mq = null;
            }
            LogHelper.WriteLog(typeof(Service1), "杰瑞上传MQ服务停止");

        }

        private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
        {
            string filename = System.IO.Path.GetFileNameWithoutExtension(e.FullPath);//文件名  
            string[] s = filename.Split(new char[] { '-' });

            DataRecvRecord record = new DataRecvRecord();
            FullDataRecvRecord(record, s, e);
            UploadMQ(record);

        }

        private void UploadMQ(DataRecvRecord record)
        {
            bool ans2 = false;
            //挂MQ 实时显
            try
            {
                DIY.MQParam mqparam = mq.MakeMqParam(record);
                string msg = mq.MakeXml(mqparam);
                ans2 = mq.SendMQMessage(msg, "ADDRESSCODE", mqparam.ADDRESSCODE);
                if (ans2 == true)
                {
                    //if (gLogDetail) Log("发送MQ消息成功:" + msg);
                    LogHelper.WriteLog(typeof(Service1), "发送MQ消息成功:" + msg);

                }
                else
                {
                    LogHelper.WriteLog(typeof(Service1), "发送MQ消息失败:" + msg);

                }
            }
            catch (Exception er)
            {
                LogHelper.WriteLog(typeof(Service1), er, er.Message);

            }
        }

        /// <summary>
        /// 处理字符串
        /// </summary>
        /// <param name="record"></param>
        /// <param name="s"></param>
        /// <param name="e"></param>
        #region FullDataRecvRecord处理字符串
        private void FullDataRecvRecord(DataRecvRecord record, string[] s, FileSystemEventArgs e)
        {
            record.P15 = s[0];
            switch (s[1])
            {
                case "东向西":
                    record.P16 = "1";
                    break;
                case "南向北":
                    record.P16 = "2";
                    break;
                case "西向东":
                    record.P16 = "3";
                    break;
                case "北向南":
                    record.P16 = "4";
                    break;
                default:
                    record.P16 = "1";
                    break;
            }
            record.P17 = s[2];
            record.P27 = s[3];
            record.P26 = s[4];

            if (s[6].Contains("无牌"))
            {
                record.P23 = "";
            }
            else
            {
                record.P23 = s[6];
            }
            switch (s[7])
            {
                case "大车":
                    record.P21 = "01";
                    break;
                case "小车":
                    record.P21 = "02";
                    break;
                default:
                    record.P21 = "99";
                    break;
            }
            record.P24 = "0";
            if (s[9].Contains("未知"))
            {
                record.P22 = "";
            }
            else
            {
                record.P22 = s[9];
            }
            record.P5 = s[10];
            record.P13 = s[11];

            record.P8 = s[12];
            record.P18 = s[13];
            string ftpName = e.FullPath.Substring(gFTPfolder.Length);
        }
        #endregion
    }
}
